import React,{Component} from 'react';

import{
    Text,
    View
} from 'react-native';
import {
  Button,
  List,
  InputItem,
  Switch,
  DatePicker,
  Toast
} from 'antd-mobile';
const Item = List.Item;

const nowTimeStamp = Date.now();
const now = new Date(nowTimeStamp);

export default class DetailComponent extends Component<{}> {

  static navigationOptions={
     title:'行程',
     headerStyle:{
       backgroundColor:'#fff',
     },
     headerTitleStyle:{
        color:'green',
        alignSelf : 'center',
     },
     cardStack: {
             gesturesEnabled: true,
     },
  }
  constructor(props){
     super(props);
     this.state = {
        data:{

        },
        time:""
     }
  }
  componentWillMount(){
      const params = this.props.navigation.state.params;
      const route_id = params.route_id;
      fetch("http://didi.spoplar.xyz/item?route_id="+route_id,{
         method:'GET',
         headers: {
               'Content-Type': 'text/json'
         }
      })
      .then((response)=>response.text())
      .then((responseData)=>{
        var str = responseData.replace(/\'/g,'"');
        var jstr = JSON.parse(str)
        if(jstr.code == 'OK'){
          this.setState({
            data:jstr.data,
            time:jstr.data.strive_time
          })
        }
      })
  }
  inputChange(value,data){
      var da = {}
      da.strive_degree = value;
      var obj = Object.assign(data,da)
      this.setState({
         data:obj
      })
  }
  changeDate(date,data){
     var hour = date.getHours()
     var minu = date.getMinutes()
     var tt = hour+':'+minu;
     var da = {}
     da.strive_time = tt;
     var obj = Object.assign(data,da)
     this.setState({
        time:tt,
        data:obj
     })

  }
  onSubmit(data){
     Toast.success(data.strive_degree);
     fetch('http://didi.spoplar.xyz/strive_degree_time',{
        method:'POST',
        headers: {
              'Content-Type': 'application/x-www-form-urlencoded'
        },
        body: "ttime="+data.strive_time+"&degree="+data.strive_degree+"&route_id="+data.route_id
     })
     .then((response)=>response.text())
     .then((responseData)=>{
       var str = responseData.replace(/\'/g,'"');
       str = str.replace(/\:\}/g,':""}');
       var jstr = JSON.parse(str)
       if(jstr.code == 'OK'){
         Toast.success(jstr.code)
       }else{
         Toast.error(jstr.code)
       }
     })
  }
  swit(checked,route_id,url_type){
    var swit = '0';
    if(checked){
      swit = '1';
    }

    fetch("http://didi.spoplar.xyz/"+url_type+"_switch?route_id="+route_id+'&swit='+swit,{
       method:'GET',
       headers: {
             'Content-Type': 'text/json'
       }
    })
    .then((response)=>response.text())
    .then((responseData)=>{
      var str = responseData.replace(/\'/g,'"');
      str = str.replace(/\:\}/g,':""}');
      //Toast.success(str);
      var jstr = JSON.parse(str)
      if(jstr.code == 'OK'){
        var da={};
        if(url_type == 'notice'){
          if(checked){
            da.is_notice=1;
          }else{
            da.is_notice=0;
          }
        }else{
          if(checked){
            da.is_strive=1;
          }else{
            da.is_strive=0;
          }
        }
        var obj = Object.assign(this.state.data,da)
        this.setState({
          data:obj
        })
      }
    })
  }
   render(){
      const tt = this.state.time.split(':');
      const hour = tt[0]
      const minu = tt[1]
      const deg = this.state.data.strive_degree+''
      return (
        <View>
          <List renderHeader={() => '出发时间:'+this.state.data.departure_time}>
            <Item extra={this.state.data.from_name} wrap='true'>出发地</Item>
            <Item extra={this.state.data.to_name} wrap='true'>目的地</Item>
          </List>

          <List renderHeader={() => '设置'}>
             <DatePicker
              mode='time'
              value={ new Date(now.getFullYear(),now.getMonth(),now.getDate(),hour,minu,0)}
              onChange={(date)=>{this.changeDate(date,this.state.data)}}
             >
              <Item arrow="horizontal">截止时间</Item>
             </DatePicker>
             <InputItem
              type="number"
              defaultValue={deg}
              onChange={(value)=>this.inputChange(value,this.state.data)}
              >接单阀值</InputItem>
               <Item>
                  <Button type="primary" size="large" inline onClick={()=>this.onSubmit(this.state.data)}>Submit</Button>
               </Item>
          </List>
          <List renderHeader={() => '开关'}>
            <Item extra={<Switch
              checked={this.state.data.is_notice > 0 ? true :false }
              onChange={(checked)=>this.swit(checked,this.state.data.route_id,'notice')}
              />}>是否提醒</Item>
            <Item extra={<Switch
              checked={this.state.data.is_strive>0? true :false}
              onChange={(checked)=>this.swit(checked,this.state.data.route_id,'strive')}
              />}>是否接单</Item>
          </List>
        </View>

      )
   }


}
